package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.common.exception.CustomException;
import com.spc.car.data.domain.project.dto.GetProjectRelationshipsDTO;
import com.spc.car.data.domain.project.po.CreateProjectRelationPO;
import com.spc.car.data.domain.project.po.DropProjectRelationPO;
import com.spc.car.data.domain.project.po.GetProjectRelationshipsPO;
import com.spc.car.data.domain.project.po.ProjectTenderingProjectPO;
import com.spc.car.data.entity.project.Project;
import com.spc.car.data.entity.project.ProjectTenderingProject;
import com.spc.car.data.mapper.project.ProjectTenderingProjectMapper;
import com.spc.car.data.service.project.ProjectService;
import com.spc.car.data.service.project.ProjectTenderingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 招标项目与项目关系表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-09
 */
@Service
public class ProjectTenderingProjectServiceImpl extends ServiceImpl<ProjectTenderingProjectMapper, ProjectTenderingProject> implements ProjectTenderingProjectService {


    @Autowired
    private ProjectService projectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int establishRelationships(CreateProjectRelationPO po) {
        String projectTenderingCode = po.getProjectTenderingCode();
        List<ProjectTenderingProject> existedList = list(new QueryWrapper<ProjectTenderingProject>()
                        .eq("project_tendering_code",projectTenderingCode));
        List<String> projectCodeList = po.getProjectList().stream()
                .map(ProjectTenderingProjectPO::getProjectCode).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(existedList)) {
            projectCodeList.addAll(
                    existedList.stream()
                            .map(ProjectTenderingProject::getProjectCode)
                            .collect(Collectors.toList()));
        }
        List<Project> projectList = projectService.list(new QueryWrapper<Project>()
                .in("project_code", projectCodeList));
        if (CollectionUtils.isEmpty(projectList)) {
            throw new CustomException("没有匹配的项目");
        }

        if (projectList.stream()
                .map(Project::getProjectType)
                .distinct()
                .collect(Collectors.toList()).size() > 1) {
            throw new CustomException("项目类型冲突");
        }

        remove(new QueryWrapper<ProjectTenderingProject>()
                .eq("project_tendering_code",projectTenderingCode)
        );
        List<ProjectTenderingProject> projectTenderingProjects = new ArrayList<>();
        projectCodeList.stream().distinct().forEach(projectCode ->{
            ProjectTenderingProject projectTenderingProject = new ProjectTenderingProject();
            projectTenderingProject.setProjectTenderingCode(projectTenderingCode);
            projectTenderingProject.setProjectCode(projectCode);
            projectTenderingProjects.add(projectTenderingProject);
        });
        if (saveBatch(projectTenderingProjects)){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public boolean cancelRelationships(DropProjectRelationPO po) {
        return removeById(po.getId());
    }

    @Override
    public Page<GetProjectRelationshipsDTO> listProjectRelationshipsByCode(GetProjectRelationshipsPO<GetProjectRelationshipsDTO> po) {
        Page page = po.getPage();
        Long star = page.getSize() * (page.getCurrent() - 1L);
        List<GetProjectRelationshipsDTO> list = baseMapper.listProjectRelationships(page.getSize(), po, star);
        page.setRecords(list);
        page.setTotal(baseMapper.listProjectRelationshipsCount(po));
        return page;
    }
}
